#include "BlockingQueue.h"

#include <iostream>
using namespace std;

// CBlockingQueueSingle<int,LockEvent> mgr;
// CBlockingQueueCirculate<int,LockMutex> mgr(40);
 CBlockingQueueDouble<int,LockCritSec> mgr(40);
int g_Total = 200;

DWORD WINAPI ThreadProc0(LPVOID lpParameter)
{
	while (g_Total >= 0)
	{
// 		if(!mgr.Put(g_Total--))
// 			Sleep(10);
		if(mgr.Put(g_Total))
		{
			g_Total--;
		}
	}
	mgr.EndOfStream();
	return 0;
}

DWORD WINAPI ThreadProc1(LPVOID lpParameter)
{
	int nCount = 100;
	while(!mgr.IsDone())
	{
		int x;
		if(mgr.Get(x))
		{
			cout << x << "\t";
			if(--nCount %10 == 0)
				cout << endl;
		}
	}
	return 0;
}

int main()
{
	HANDLE hThread[2];
	DWORD dwThreadID1;
	hThread[1] = CreateThread(NULL,0,ThreadProc1,NULL,0,&dwThreadID1);


	DWORD dwThreadID0;
	hThread[0] = CreateThread(NULL,0,ThreadProc0,NULL,0,&dwThreadID0);

// 	Sleep(10);

 	WaitForMultipleObjects(2,hThread,TRUE,INFINITE);
// 	int x = 0;
// 	int nCount = 100;
// 	while(mgr.Size())
// 	{
// 		mgr.Get(x);
// 		cout << x << "\t";
// 		if(--nCount %10 == 0)
// 			cout << endl;
// 	}
// 	mgr.Print();

	return 0;
}